<?php
namespace Lyricist\Controller;
use  Common\Controller\LyricistController;
class LoginController extends LyricistController {

    /**
     * 小程序登录
     * @param string
     * @return string
     */
    public function wxlogin(){


        $code   =  I('post.code','','htmlspecialchars');
        $appid  =  I('post.appid','','htmlspecialchars');
        $scene  =  I('post.scene','','htmlspecialchars');

        //来源方式黑名单
        $blacklist  =   array('1011','1025');

        switch ($appid)
        {          
                case "wx752ceb8e4e1200e3"; //测试
                    $secret = '7e278a7487bce9741129898c6e7a6995';
            break;
                  
                case "wx60ecd8c9cdbd87a4"; //测试
                    $secret = 'ac459b92d6e61d354844c1b65c1f9511';
            break;


                default;

        }
		$isopen = 0;
        $info = $_POST['info'];

        if(!$code||!$appid||!$secret||!$info){
            $data = array();

            $this->returnData($data,$msg='参数错误',$code=401);

        }


        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";

        $res = $this->curlget($url);

        if (!$res['openid']) {
            $this->returnData($res,$msg='openid获取失败',$code=100);
        }

        $info= json_decode($info,true);

        $nickName = $info['nickName'];
        $avatarUrl = $info['avatarUrl'];
        $gender = $info['gender'];
        $province = $info['province'];
        $city = $info['city'];
        $country = $info['country'];
        $language = $info['language'];

        $status     =   1;  //1表示正常 -1表示异常（黑白名单用）
        $lock       =   1;  //锁住（1表示正常 -1表示锁住）


            $isnew = 0;
        if ($res['openid']){

            $where= array(
                'openid'=>$res['openid']
            );

            $field  =   array('uid','nickname','gender','city','province','country','avatarurl','openid','scene','status','lock');
            $user   =   M('lyricist_member')->field($field)->where($where)->find(); //判断是否有用户信息

            $play_num = M('lyricist_config')->where(array('category'=>'lyricist'))->getField('play_num'); //配置文件

            if (empty($play_num)){
                $play_num = 10;
            }

            if ($user){
                $user_id = $user['uid'];
                $isnew = 0;
                $userinfo = array(
                    'nickname'=>$nickName,
                    'gender'=>$gender,
                    'city'=>$city,
                    'province'=>$province,
                    'country'=>$country,
                    'avatarurl'=>$avatarUrl,
                    'language'=>$language,
                    'uinionid' =>$res['unionId']||'',
                    'login_time' =>time(),
                    'openid' =>$res['openid'],
                    'appid'=>$appid,
                    'session_key'=>$res['session_key'],
                );

                if (! empty($user['status']))
                {
                    $status     =   $user['status'];
                }

                if (! empty($user['lock']))
                {
                    $lock   =   $user['lock'];
                }

                if (! empty($scene) && empty($user['scene']))
                {
                    $userinfo['scene']  =   $scene;
                    if (in_array( $scene, $blacklist))
                    {
                        $userinfo['status']  =   -1;    //黑名单
                        $status              =   -1;
                        $userinfo['game_num'] = $play_num;
                    }
                }

                M('lyricist_member')->where($where)->save($userinfo);

            }else{
                $isnew = 1;
                $userinfo = array(
                    'nickname'=>$nickName,
                    'gender'=>$gender,
                    'city'=>$city,
                    'province'=>$province,
                    'country'=>$country,
                    'avatarurl'=>$avatarUrl,
                    'language'=>$language,
                    'session_key'=>$res['session_key'],
                    'uinionid' =>$res['unionId']||'',
                    'login_time' =>time(),
                    'openid' =>$res['openid'],
                    'create_time'=>time(),
                    'appid'=>$appid,
                );


                if (! empty($scene))
                {
                    $userinfo['scene']  =   $scene;

                    if (in_array( $scene, $blacklist))
                    {
                        $userinfo['status']  =   -1;    //黑名单
                        $status              =   -1;
                        $userinfo['game_num'] = $play_num;
                    }
                }

                $user_id= M('lyricist_member')->data($userinfo)->add();

            }

        }

        $session_id =   $this->getSessionId();

        $data = array(
            'openid'=>$res['openid'],
            'session_id' =>$session_id,
            'uid' =>$user_id,
            'lock'=>$lock,
            'status'=>$status,
            'isnew'=>$isnew,
            'isopen'=>$isopen,
        );

        $this->returnData($data,'成功',200);

    }

    public function clickad(){

        $appid = I('post.appid','','htmlspecialchars');
        $uid = I('post.uid','','htmlspecialchars');
        $location = I('post.location','','htmlspecialchars');
        $clickId = I('post.clickId','','htmlspecialchars');

        if (!$appid){
            $this->returnData('','非法操作');
        }

        $data = array(
            'appid'=>$appid,
            'uid'=>$uid,
            'create_time'=>date('Y-m-d H:i:s',time()),
            'clicknum'=>'1',
            'location'=>$location,
            'clickid'=>$clickId,
            'time'=>time(),
        );

        M('lyricist_clickad')->data($data)->add();

        $this->returnData();

    }



}
